import requests

def assert_json_fields(url, field_types):
    resp = requests.get(url, timeout=10)
    resp.raise_for_status()
    data = resp.json()
    errors = []
    for field, expected_type in field_types.items():
        value = data.get(field, None)
        if value is None:
            errors.append(f'字段 {field} 为空')
        elif not isinstance(value, expected_type):
            errors.append(f'字段 {field} 类型错误，期望 {expected_type.__name__}，实际 {type(value).__name__}')
    return errors

if __name__ == '__main__':
    url = input('请输入API地址: ')
    print('请输入要校验的字段及类型，例如: id=int,name=str')
    field_str = input('字段及类型: ')
    field_types = {}
    for item in field_str.split(','):
        k, v = item.split('=')
        field_types[k.strip()] = eval(v.strip())
    errors = assert_json_fields(url, field_types)
    if errors:
        print('校验失败:')
        for err in errors:
            print(err)
    else:
        print('所有字段校验通过。') 